//
//  YXWebViewController.m
//  CaoShi
//
//  Created by aa on 2018/4/28.
//  Copyright © 2018年 杨旭. All rights reserved.
//

#import "YXWebViewController.h"
#import "WebviewProgressLine.h"
#import "YXErrorView.h"
@interface YXWebViewController ()<UIWebViewDelegate>
@property (nonatomic ,strong) WebviewProgressLine *progressLine;
@property (nonatomic ,strong) UIWebView *web;
@property (nonatomic ,strong) YXErrorView *errorView;
@end

@implementation YXWebViewController
- (UIWebView *)web {
    if (!_web) {
        UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, ISiPhoneX?88:64, kScreenWidth, kScreenHeight-64)];
        // UIWebView加载过程中，在页面没有加载完毕前，会显示一片空白。为解决这个问题，方法如下：让UIWebView背景透明。
        web.backgroundColor = [UIColor clearColor];
        web.scrollView.bounces = YES;
        web.scalesPageToFit = YES;//设置网页自动适应
        web.scrollView.showsVerticalScrollIndicator = YES;//设置滚动条隐藏(YES)
        web.opaque = NO;
        [web setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"webbg.png"]]];
        [self.view addSubview:web];
        _web = web;
    }
    return _web;
}

- (YXErrorView *)errorView {
    if (!_errorView) {
        _errorView = [[YXErrorView alloc] initWithFrame:(CGRectMake(0, 64, kScreenWidth, kScreenHeight)) ImageName:@"w_no_blue" Title:@"网络不给力"];
        _errorView.backgroundColor = [UIColor whiteColor];
        YXWeakSelf
        [_errorView setClickUpdataBlock:^{
            NSLog(@"xxxx");
            NSURL *url = [NSURL URLWithString:weakSelf.url];
            [weakSelf.web loadRequest:[NSURLRequest requestWithURL:url]];
            [weakSelf.errorView removeFromSuperview];

        }];
    }
    return _errorView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"web";
    
    self.progressLine = [[WebviewProgressLine alloc] initWithFrame:CGRectMake(0, 64, kScreenHeight, 3)];
    self.progressLine.lineColor = [UIColor orangeColor];
    self.progressLine.width = 0.0f;
    [self.view addSubview:self.progressLine];
    
    [self loadData];

}
- (void)loadData {
    
    self.web.delegate = self;
    NSURL *url = [NSURL URLWithString:self.url];
    [self.web loadRequest:[NSURLRequest requestWithURL:url]];
    

}


// 网页开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView {
    [MBProgressHUD showMessage:@"稍等,玩命加载中"];
    [self.progressLine startLoadingAnimation];
}
// 网页完成加载
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [MBProgressHUD hideHUD];
    [self.progressLine endLoadingAnimation];
}
// 网页加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [MBProgressHUD hideHUD];
    [self.progressLine endLoadingAnimation];
    NSLog(@"error == %@",error);
    [self.view addSubview:self.errorView];
    
}

@end
